package org.example.everyday;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/6/12 0012 18:22
 */
public class Lc890 {
    public static void main(String[] args) {
        Lc890 lc890 = new Lc890();

       String[] words = {"abc","deq","mee","aqq","dkd","ccc"};
       String pattern = "abb";
       List<String> res = lc890.findAndReplacePattern(words,pattern);
       System.out.println(res);


    }
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> ans = new ArrayList<>();
        for (String word : words){
            if(match(word,pattern) && match(pattern,word)){
                ans.add(word);
            }
        }
        return ans;
    }

    private boolean match(String pattern, String word) {
        Map<Character,Character> map = new HashMap<Character, Character>();
        for (int i = 0; i < word.length(); i++) {
            char x = word.charAt(i), y = pattern.charAt(i);
            if(!map.containsKey(x)){
                map.put(x,y);
            }else if (map.get(x) != y){
                return false;
            }
        }
        return true;

    }

}
